package com.lune.job.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lune.job.pojo.Project;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IPostService;
import com.lune.job.service.IProjectService;
import com.lune.job.service.IResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/project")
@Api(tags = "项目经历管理")
public class ProjectController {

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IResumeService resumeService;

    @PostMapping("/")
    @ApiOperation(value = "添加项目经历")
    public RespBean create(@RequestBody Project project) {
        if (projectService.save(project)) {
            return RespBean.success("添加项目经历成功");
        } else {
            return RespBean.error("添加项目经历失败");
        }
    }

    @ApiOperation(value = "删除单个项目经历")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        if (projectService.removeById(id)) {
            return RespBean.success("删除项目经历成功");
        }
        return RespBean.error("删除项目经历失败");
    }

    @ApiOperation(value = "批量删除项目经历")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (projectService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除项目经历成功");
        } else {
            return RespBean.error("批量删除项目经历失败");
        }
    }

    @ApiOperation(value = "修改项目经历")
    @PutMapping("/")
    public RespBean update(@RequestBody  Project project) {
        if (projectService.updateById(project)) {
            return RespBean.success("修改项目经历成功");
        } else {
            return RespBean.error("修改项目经历失败");
        }
    }

    @ApiOperation(value = "根据id查询项目经历")
    @GetMapping("/detail")
    public Project detail(Integer id) {
        return projectService.getById(id);
    }

    @ApiOperation(value = "分页查询项目经历")
    @PostMapping("/page")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestBody  Project project) {
        IPage<Project> intentionIPage = projectService.queryPage(currentPage, size, project);
        intentionIPage.getRecords().forEach(item -> {
            item.setResume(resumeService.getById(item.getResume_id()));
        });
        return new ResPageBean(intentionIPage.getTotal(), intentionIPage.getPages(), intentionIPage.getRecords());
    }


    @ApiOperation(value = "条件查询项目经历")
    @PostMapping("/query")
    public List<Project> queryDict(
            @RequestBody  Project project) {
        List<Project> projects = projectService.queryByCon(project);
        return projects;
    }
}
